using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponExplorerList : MonoBehaviour
{

    public CallbackWeapon m_OnWeaponSelectedCallback;
    public CallbackWeapon m_OnWeaponDeleteCallback;
    public GameObject m_WeaponExplorerContentParent;
    public WeaponExplorerItem m_ItemPrefab;
    public List<WeaponExplorerItem> m_Items = new List<WeaponExplorerItem>();
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void Init()
    {
        ClearItems();
        foreach(var i in ItemManager.Instance.m_Weapons)
        { 
            AddItem(i);
        }
    }

    public void AddItem(Weapon weapon)
    {
        var item = Instantiate(m_ItemPrefab);
        item.Init(weapon);
        item.transform.SetParent(transform);
        item.m_ExplorerList = this;
        m_Items.Add(item);
    }

    void ClearItems()
    {
        for(int i=0; i<m_Items.Count; ++i)
        {
            Destroy(m_Items[i].gameObject);
        }
        m_Items.Clear();
    }
    public void OnWeaponItemOpenByBlueprintClicked(Weapon weapon)
    {
        if (m_OnWeaponSelectedCallback != null)
            m_OnWeaponSelectedCallback(weapon);
    }
    public void OnWeaponDeleteClicked(Weapon weapon)
    {
        foreach(var i in m_Items)
        {
            if(i.m_Weapon == weapon)
            {
                var item = i;
                m_Items.Remove(i);
                Destroy(item.gameObject);
                break;
            }
        }
        ItemManager.Instance.RemoveWeapon(weapon);
    }
}
